package com.lovo.invoicing.controller;

import com.lovo.invoicing.bean.BuyCartBean;
import com.lovo.invoicing.bean.GoodsBean;
import com.lovo.invoicing.bean.UserBean;
import com.lovo.invoicing.service.IBuyCartService;
import com.lovo.invoicing.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/buyCart")
public class BuyCartController {
    @Autowired
    IBuyCartService buyCartService;
    @Autowired
    IGoodsService goodsService;

    @RequestMapping("/addIntoCart")
    public String addIntoCart(GoodsBean goodsBean, @RequestParam int buyNo, HttpServletRequest request) {
        goodsService.addGoods(goodsBean);
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        buyCartService.addIntoCart(goodsBean,buyNo,u.getUserId());
        return "success";
    }

    @RequestMapping("/removeBuyCart")
    public String removeBuyCart(int buyCartId){
        buyCartService.deleteBuyCart(buyCartId);
        return "success";
    }

    @RequestMapping("/modifyBuyCart")
    public String modifyBuyCart(@RequestBody BuyCartBean buyCartBean){
        buyCartService.updateBuyCart(buyCartBean);
        return "success";
    }

    @RequestMapping("/findBuyCarts")
    public List<BuyCartBean> findBuyCarts(HttpServletRequest request){
        UserBean u = (UserBean) request.getSession().getAttribute("user");
        return buyCartService.queryBuyCarts(u.getUserId());
    }
}
